package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.OrderService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.OrderDto;
import com.huilian.iotbox.data.vo.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 谢洛涛
 * @date 2021/1/18 11:28
 */
@RestController
@RequestMapping("/api/order")
public class OrderController extends UserInfoController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单查询-公众号
     *
     * @param orderDto
     * @param request
     * @return
     */
    @PostMapping("/findOrderList")
    public ServerResponse findOrderList(@RequestBody OrderDto orderDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(orderService.findOrderList(orderDto));
    }

    /**
     * 订单查询-小程序
     *
     * @param orderDto
     * @param request
     * @return
     */
    @PostMapping("/findOrderListByApplet")
    public ServerResponse findOrderListByApplet(@RequestBody OrderDto orderDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return orderService.findOrderListByApplet(orderDto);
    }

    /**
     * 订单查询，页头统计-小程序
     *
     * @param orderDto
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/findOrderStatisticsByApplet")
    public ServerResponse findOrderStatisticsByApplet(@RequestBody OrderDto orderDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return orderService.findOrderStatisticsByApplet(orderDto);
    }


    @PostMapping("/statisticsDayIncome")
    public ServerResponse statisticsDayIncome(@RequestBody OrderDto orderDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return orderService.statisticsDayIncome(orderDto);
    }

    @GetMapping("/findOneOrderByOutTradeNo")
    public ServerResponse findOneOrderByOutTradeNo(OrderDto orderDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ServerResponse.createByErrorMessage("获取商户信息失败");
        }
        orderDto.setUserInfo(userInfo);
        return orderService.findOneOrderByOutTradeNo(orderDto);
    }

    @PostMapping("/checkOrderCode")
    public ServerResponse checkOrderCode(@RequestBody OrderDto orderDto,HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(orderService.checkOrderCode(orderDto));
    }

    @PostMapping("/updateRemark")
    public ServerResponse updateRemark(@RequestBody OrderDto orderDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return orderService.updateRemark(orderDto);
    }

    /**
     * 首页头部统计数据
     * */
    @PostMapping("/findHeadStatisticsData")
    public ServerResponse findHeadStatisticsData(@RequestBody OrderDto orderDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        orderDto.setUserId(userInfo.getId());
        return orderService.findHeadStatisticsData(orderDto);
    }
    @GetMapping("/findReturnedItems")
    public ServerResponse findReturnedItems(@Param("orderId") Long orderId){
        return orderService.findReturnedItems(orderId);
    }
}
